package com.emptyMe.org.service.imp;

import com.emptyMe.org.domain.Shop;
import com.emptyMe.org.mapper.EmployeeMapper;
import com.emptyMe.org.mapper.ShopMapper;
import com.emptyMe.org.query.ShopQuery;
import com.emptyMe.org.service.IShopService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShopServiceImpl implements IShopService {
    @Autowired
    private ShopMapper mapper;
    @Autowired
    private EmployeeMapper empMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void save(Shop shop) {
        empMapper.save(shop.getAdmin());
        mapper.save(shop);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void remove(Long id) {
        mapper.remove(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void removeMany(List<Shop> shops) {
        for (Shop shop : shops) {
            mapper.remove(shop.getId());
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void update(Shop shop) {
        mapper.update(shop);
    }

    @Override
    public PageInfo<Shop> searchData(ShopQuery query) {
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        List<Shop> departments = mapper.searchData(query);
        Page<Shop> pages = (Page<Shop>) departments;
        return pages.toPageInfo();
    }
}
